package com.ljx.funchat.user.dao;

import com.ljx.funchat.common.domain.vo.req.CursorPageBaseReq;
import com.ljx.funchat.common.domain.vo.resp.CursorPageBaseResp;
import com.ljx.funchat.common.utils.CursorUtils;
import com.ljx.funchat.user.domain.entity.UserApply;
import com.ljx.funchat.user.domain.enums.UserApplyReadEnum;
import com.ljx.funchat.user.domain.enums.UserApplyStatusEnum;
import com.ljx.funchat.user.domain.vo.req.UserFriendHandleReq;
import com.ljx.funchat.user.mapper.UserApplyMapper;
import com.ljx.funchat.user.service.IUserApplyService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 申请表 服务实现类
 * </p>
 *
 * @author LiJunXi
 * @since 2024-02-16
 */
@Service
public class UserApplyDao extends ServiceImpl<UserApplyMapper, UserApply> {

    public UserApply getByUidAndTargetId(Long uid, Long targetId, Integer type) {
        return lambdaQuery().eq(UserApply::getUid, uid)
                .eq(UserApply::getTargetId, targetId)
                .eq(UserApply::getType, type)
                .one();
    }

    public void updateByApplyId(Long uid, UserFriendHandleReq req) {
        lambdaUpdate().eq(UserApply::getId, req.getApplyId())
                .eq(UserApply::getTargetId, uid)
                .set(UserApply::getStatus, req.getStatus())
                .set(UserApply::getReadStatus, UserApplyReadEnum.READ.getStatus())
                .update();
    }

    public CursorPageBaseResp<UserApply> getApplyPage(Long uid, CursorPageBaseReq req) {
        return CursorUtils.getCursorPageByMysql(this, req, wrapper -> {
            wrapper.eq(UserApply::getTargetId, uid);
        }, UserApply::getId);
    }

    public Integer getUnReadCount(Long targetId) {
        return lambdaQuery().eq(UserApply::getTargetId, targetId)
                .eq(UserApply::getReadStatus, UserApplyReadEnum.NO_READ.getStatus())
                .count();
    }
}
